import bezier from './bezier';
/**
 * @desc 工具库
 */
class Util {
  formatNumber(num) {
    let n = num;
    if (num > 10000) {
      n = parseInt(n / 10000, 10) + 'W+';
    }
    return n;
  }
  cutOutString(str) {
    let length = str.length;
    let cutStr = [];
    for (let i = 0; i < parseInt(length / 25, 10); i++) {
      cutStr.push(str.substr(i * 25, 25));
    }
    return cutStr;
  }
  /**
   * @desc get random id
   * @param {number} randomLength
   */
  getRandomID(randomLength = 8) {
    return Number(
      Math.random()
        .toString()
        .substr(3, randomLength || 8) + Date.now(),
    ).toString(36);
  }

  /**
   * 生成随机数字
   * @param {number} min 最小值（包含）
   * @param {number} max 最大值（不包含）
   */
  randomNumber(min = 0, max = 100) {
    return Math.min(Math.floor(min + Math.random() * (max - min)), max);
  }

  /**
   * @desc 计算坐标的总长度和每两点的长度占比 和 角度
   */
  countPaths(path, pathType, dotsNumber) {
    let allPathLength = 0;
    let pathThan = [];
    // 曲线
    if (pathType === 'bezier') {
      path = bezier.getBezierPoints(dotsNumber, path[0], path[1], path[2]);
    }
    // console.log(path, 'path1');
    path.forEach((d, i) => {
      let show = true;
      if (i !== 0) {
        const [x1, y1] = path[i - 1];
        const [x2, y2] = d;
        const angle = this.getAngle([x1, y1], [x2, y2]);
        let xdiff = x2 - x1;
        let ydiff = y2 - y1;
        let len = Math.pow(xdiff * xdiff + ydiff * ydiff, 0.5);
        if (i === 5 || i === 9) {
          show = false;
        }
        pathThan.push({
          show: show,
          than: len,
          angle,
          path: [x2, y2],
        });
        allPathLength += len;
      } else {
        const [x1, y1] = d;
        pathThan.push({
          show: show,
          than: 0,
          angle: 0,
          path: [x1, y1],
        });
      }
    });
    let total = 0;
    pathThan.map((d) => {
      d.than = d.than / allPathLength;
      total += d.than;
      d.percent = total;
      return d;
    });
    return {
      pathThan,
      allPathLength,
    };
  }

  // 判断向限， return string 1,12,2,23,3,34,4,41 两位数的表示在边界上
  quadrant(p1, p2) {
    let x = '';
    if (p1[0] <= p2[0] && p1[1] >= p2[1]) {
      x += 1;
    }
    if (p1[0] >= p2[0] && p1[1] >= p2[1]) {
      x += 2;
    }
    if (p1[0] >= p2[0] && p1[1] <= p2[1]) {
      x += 3;
    }
    if (p1[0] <= p2[0] && p1[1] <= p2[1]) {
      x += 4;
    }
    return x;
  }

  // 获取角度 1,12,2,23,3,34,4,41
  getAngle(start, end) {
    const [x1, y1] = start;
    const [x2, y2] = end;
    let angle = Math.atan2(y2 - y1, x2 - x1);
    angle = (angle * 180) / Math.PI;
    // console.log('angle', angle);
    return angle;
  }
}

export default new Util();
